package com.wuhan.template.module.home.activity;

import android.support.annotation.NonNull;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.NewsBaseAdapter;
import com.wuhan.template.module.home.entity.HomeNewsEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class NewsListActivity extends BaseActivity {

    @BindView(R.id.news_title)
    protected TextView title;
    @BindView(R.id.news_image)
    protected ImageView image;
    @BindView(R.id.news_pullListView)
    protected PullToRefreshListView listView;
    private String outlink;
    private ImageLoader loader;
    private DisplayImageOptions options;

    private int offset = 0;
    private List<HomeNewsEntity> news = new ArrayList<>();
    private NewsBaseAdapter baseAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_list;
    }

    @Override
    protected void initViews() {
        outlink = getIntent().getStringExtra("outlink").replace("special#", "");
        title.setText(getIntent().getStringExtra("newsTitle"));
        final String imageUrl = getIntent().getStringExtra("imageUrl");
        loader = ((BaseApplication) (this.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();

        image.setScaleType(ImageView.ScaleType.FIT_XY);
        loader.displayImage(imageUrl, image, options);

        baseAdapter = new NewsBaseAdapter(this, news);
        listView.setAdapter(baseAdapter);

        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                listView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");
                SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                long timeMillis = System.currentTimeMillis();
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新时间:" + format.format(timeMillis));
                offset = 0;
                news.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                listView.getLoadingLayoutProxy().setRefreshingLabel("正在加载");
                listView.getLoadingLayoutProxy().setPullLabel("加载更多");
                listView.getLoadingLayoutProxy().setReleaseLabel("释放加载更多");
                offset += 20;
                loadData();
            }
        });
    }

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.HOME_NEWS + "&column_id=" + outlink + "&offset=" + offset, null);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if (message.type.equals(Urls.HOME_NEWS + "&column_id=" + outlink + "&offset=" + offset)) {
            Gson gson = new Gson();
            HomeNewsEntity[] homeNewsEntities = gson.fromJson(message.result, HomeNewsEntity[].class);

            news.addAll(Arrays.asList(homeNewsEntities));

            baseAdapter.notifyDataSetChanged();
            listView.onRefreshComplete();
        }
    }
}
